package io.renren.modules.userdata.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.SnowFlakeIdGenerator;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.userdata.entity.TLabelEntity;
import io.renren.modules.userdata.service.TLabelService;
import io.renren.modules.userdata.service.UserDataManagerService;
import io.renren.modules.userdata.service.impl.MultDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author owen
 * @email zhaoouyang@qq.com
 * @date 2018-10-31 21:51:21
 */
@RestController
@RequestMapping("userdata/tlabel")
public class TLabelController {

    @Autowired
    private TLabelService tLabelService;

    @Autowired
    private UserDataManagerService userDataManagerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("userdata:tlabel:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tLabelService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("userdata:tlabel:info")
    public R info(@PathVariable("id") Long id){
        TLabelEntity tLabel = tLabelService.selectById(id);

        return R.ok().put("tLabel", tLabel);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("userdata:tlabel:save")
    public R save(@RequestBody TLabelEntity tLabel){
        tLabel.setCreateTime(new Date());
        tLabel.setUpdateTime(new Date());
        tLabel.setLabelId(String.valueOf(SnowFlakeIdGenerator.getDefaultNextId()));
        tLabelService.insert(tLabel);
        userDataManagerService.saveData(tLabel);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("userdata:tlabel:update")
    public R update(@RequestBody TLabelEntity tLabel){
        ValidatorUtils.validateEntity(tLabel);
        tLabel.setUpdateTime(new Date());
        tLabelService.updateAllColumnById(tLabel); //全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("userdata:tlabel:delete")
    public R delete(@RequestBody Long[] ids){
        tLabelService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * clear数据
     */
    @RequestMapping("/clear")
    @RequiresPermissions("userdata:tlabel:clear")
    public R clear(@RequestBody Long[] ids){
        List<Long> list=Arrays.asList(ids);
        for(Long labelId:list){
            TLabelEntity tLabelEntity=tLabelService.selectById(labelId);
            if(tLabelEntity!=null){
                userDataManagerService.clearData(tLabelEntity.getUserId());
            }
        }
        return R.ok();
    }

    /**
     * 回滚数据
     */
    @RequestMapping("/rollBackData")
    @RequiresPermissions("userdata:tlabel:clear")
    public R rollBackData(@RequestBody Long[] ids){
        List<Long> list=Arrays.asList(ids);
        if(!CollectionUtils.isEmpty(list)){
            for(Long labelId: list){
                TLabelEntity tLabelEntity=tLabelService.selectById(labelId);
                if(tLabelEntity!=null){
                    userDataManagerService.rollBackData(tLabelEntity.getUserId(),labelId);
                }
            }
        }

        return R.ok();
    }



}
